Need help for array when no checkbox is selected [closed]
Posted
by
darlene
on Programmers
See other posts from Programmers
or by darlene
Published on 2012-10-22T20:05:55Z
Indexed on
2012/10/22
23:17 UTC
Read the original article
Hit count: 179
php
Hi guys I have created a form that has checkboxes, the code works great when a checkbox is selcted however when no checkboxes are selected I get errors such as "Undefined index: onemon in and Warning: Invalid argument supplied for foreach()" Please help:)
FORM
1. Had a cold, fever or headache?
input type="checkbox" name="onemon[]" value="q1"
if(isset($_GET['onemon']) && $_GET['onemon']= "q1")
2. Had dental extractions or teeth cleaning? input type="checkbox" name="onemon[]" value="q2" if(isset($_GET['onemon']) && $_GET['onemon']= "q2")
3. Had minor outpatient sugery? input type="checkbox" name="onemon[]" value="q3" if(isset($_GET['onemon']) && $_GET['onemon']= "q3")
4. Had any vaccines? input type="checkbox" name="onemon[]" value="q4" if(isset($_GET['onemon']) && $_GET['onemon']= "q4")
PHP
$date = date('Y-m-d');
$onemon = $_POST ['onemon'];
{ $con = mysql_connect("localhost","root","") or die ("Could not connect to DB Server");
$db_selected = mysql_select_db("nbtsdb", $con) or die("Could not locate the DB");
// You have to loop through the array of checked box values ...
$ques=""; foreach($onemon as $entry){ $ques .= $entry.","; }
//$addPersonalSql holds the insert SQL query string
$query = "INSERT INTO defferal(defid, intdate, onemonth)
VALUES('','$date','$ques')";
$addMemberSql = $query or die ("Unable to Insert Data into defferal Table");
mysql_query($addMemberSql);
}
© Programmers or respective owner